package edu.msse.signup.ui;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.validator.routines.DateValidator;

import edu.msse.signup.service.EventService;
import edu.msse.signup.service.PositionService;
import edu.msse.signup.service.TimeSlotService;
import edu.msse.signup.service.VolunteerService;
import edu.msse.signup.domain.Event;
import edu.msse.signup.domain.Position;
import edu.msse.signup.domain.TimeSlot;
import edu.msse.signup.domain.Users;

public class EventHandler {

	private EventService eventService;
	private TimeSlotService timeslotService;
	private PositionService positionService;
	private ApplicationService applicationService;
	private VolunteerService volunteerService;
	private String name;
	private String description;
	private Date startDate;
	private Date startTime;
	private Date endDate;
	private Date endTime;
	private Users coordinator;
	private Event currentEvent;
	private String currentEventId;
	
	public EventHandler() {
	}
	
	public List getMyEvents(){
		return eventService.getMyEvents(getCoordinator());
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	public void setVolunteerService(VolunteerService volunteerService) {
		this.volunteerService = volunteerService;
	}	
	
	public String createNew()
	{
		try{
			currentEvent = EventService.createEvent(name, description, startDate, startTime, endDate, endTime, getCoordinator());
			FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("currentEvent", currentEvent);
			FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("currentEventId", String.valueOf(currentEvent.getEventId()));
			} catch (Exception x) {
			applicationService.addErrorMessage(x.getMessage());
            return null;
        }
		
		return "newPosition";
	}

	public Users getCoordinator() {
		if(coordinator == null)
		{
			this.coordinator = (Users) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(loginHandler.CURRENT_USER);
		}
		
		return coordinator;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		
		this.endTime = endTime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public EventService getEventService() {
		return eventService;
	}
	public Event getCurrentEvent() {
		if (currentEvent == null){
			String eId = getCurrentEventId();
			if(eId != null){
				int eventId = Integer.parseInt(eId);
				currentEvent = eventService.getEvent(eventId);
				FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("currentEventId", eventId);						
			}
		}
		return currentEvent;
	}
	
	public List getFilledPositionsTimeslots(){
		List volunteerList = new ArrayList();
		List positions = getPositions();
		if (positions != null){
			for(int i = 0; i < positions.size(); i++){
				Position position = (Position) positions.get(i);
				PositionTimeSlotDetail volunteer = new PositionTimeSlotDetail();
				if (position.getTimeSlots().isEmpty()){
					List users = positionService.getUsers(position);
					for (int j=0; j<users.size(); j++){
						Users currentVolunteer = (Users) users.get(j);
						volunteer.setName(position.getName());
						volunteer.setSlotType("P");
						volunteer.setVolunteerName(currentVolunteer.getName());
						volunteer.setId(String.valueOf(position.getPositionId()));
						volunteer.setVolunteerId(String.valueOf(currentVolunteer.getUserId()));
						volunteerList.add(volunteer);
					}
				}
				else
				{
					List timeSlots = position.getTimeSlots();
					for(int k=0; k <timeSlots.size(); k++){
						TimeSlot timeSlot = (TimeSlot) timeSlots.get(k);
						List users = timeslotService.getUsers(timeSlot);
						for (int j=0; j<users.size(); j++){
							Users currentVolunteer = (Users) users.get(j);
							volunteer.setName(position.getName());
							volunteer.setStartTime(timeSlot.getStartTime());
							volunteer.setEndTime(timeSlot.getEndTime());
							volunteer.setSlotType("T");
							volunteer.setVolunteerName(currentVolunteer.getName());
							volunteer.setId(String.valueOf(timeSlot.getTimeSlotId()));
							volunteer.setVolunteerId(String.valueOf(currentVolunteer.getUserId()));
							volunteerList.add(volunteer);
						}
					}
					
				}
			}
		}
		
		
		return volunteerList;
	}
	
	
	
	public List getPositions(){ 
		if(getCurrentEvent() == null)
			return null;
		return eventService.getEventPositions(getCurrentEvent());
	}
	
	public String modify(){

		currentEventId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("currentEventId");;
		currentEvent = getCurrentEvent();
		name = currentEvent.getName();
		description = currentEvent.getDescription();
		startDate = currentEvent.getStartDate();
		startTime = currentEvent.getStartDate();
		endDate = currentEvent.getEndDate();
		endTime = currentEvent.getEndDate();
		coordinator = currentEvent.getEventCoordinator();
		return "modifyEventDetail";
	}

	public String update()
	{
		try{
			currentEvent = getCurrentEvent();
			
			if(currentEvent!= null){
				currentEvent.setDescription(getDescription());
				currentEvent.setName(getName());
				currentEvent.setStartDate(getStartDate());
				currentEvent.setEndDate(getEndDate());
				currentEvent.setStartTime(getStartTime());
				currentEvent.setEndTime(getEndTime());
				
				EventService.update(currentEvent);
			}	
		}
		catch(Exception e){
			applicationService.addErrorMessage(e.getMessage());
		}
				return "myEvents";
	}

	public String unRegisterUser(){
		Map requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
		PositionTimeSlotDetail pos = (PositionTimeSlotDetail)requestMap.get("pos");
		
		
		if("P".equals(pos.getSlotType()))
			volunteerService.unFillPosition(Integer.parseInt(pos.getVolunteerId()), Integer.parseInt(pos.getId()));
		else
			volunteerService.unFillTimeSlot(Integer.parseInt(pos.getVolunteerId()), Long.parseLong(pos.getId()));
		
		applicationService.addInfoMessage("Position \"" + pos.getName() + "\" has been unfilled for " + pos.getVolunteerName());
		
		return "myEvents";
	}
	
	public String getCurrentEventId() {
		return currentEventId;
	}

	public void setCurrentEventId(String currentEventId) {
		this.currentEventId = currentEventId;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}

	public void setTimeslotService(TimeSlotService timeslotService) {
		this.timeslotService = timeslotService;
	}
}
